dot_product_3d

语法:

dot_product_3d(x1, y1, z1, x2, y2, z2)


参数 描述
x1 第一个向量的x坐标。
y1 第一个向量的y坐标。
z1 第一个向量的z坐标。
x2 第二个向量的x坐标。
y2 第二个向量的y坐标。
z2 第二个向量的z坐标。


返回: Real(实数)


描述

点积是一个表示两个向量之间角关系的值,先把两个向量相乘,然后把结果相加得到。名称 "点积" 是从中心点 "·"产生,通常用于指定此操作 (另一个可替代名称 "标量积" 强调标量, 而不是矢量性质的结果)。

数学公式如下:


在二维平面中, a[x1,y1]和b[x2,2] 的点积就是 x1x2 + y1y2,但在三维中,向量a[x1,y1,z1]和b[x1,b1,z1]的点积是x1x2+y1y2 + z1z3。即, GameMaker Studio 2 中的三维点积计算公式:

a · b = (x1*x2) + (y1*y2) + (z1*z2);

点积的奇怪之处在于它与输入向量形成的角度的关系, 该角度可以表示为:

a · b = (length of a) * (length of b) * cos(angle)

也就是说, 两个向量的点积将等于这些向量之间角度的余弦, 乘以每个向量的长度。这儿有张插图:

有几件事, 我们现在可以发现任何两个向量和它们的点积之间的关系:

这对我们做游戏有什么用呢?嗯, 这种数学关系可以在相当多的情况下使用, 但最好的方式是建立一个实际的场景, 自己看看发生了什么。One of the simplest ways to do this is to generate a simple "height" check for an enemy in, say, a platform game so that the enemy will "see" the player if they are above the plane formed by the enemy normal vector and the 3d floor.

Basically, we are getting the vector normal from the enemy perpendicular to the floor and then we are getting the vector of the player to the enemy. We will then get the dot_product of these vectors, and if the result is positive the player is "above" the enemy floor plane and if it is negative he is below. 下面的示例中提供了要完成此工作的实际代码。


例如:

var x1, y1, x2, y2;
x1 = 0;
y1 = 1;
z1 = 0;
x2 = o_Player.x - x;
y2 = o_Player.y - y;
z2 = o_Player.z - z; if dot_product_3d(x1, y1, z1, x2, y2, z2) > 0 above=true else above=false;

上面的代码创建了一个沿着y轴的单位向量,然后获取玩家对象 "o _ player"的向量 。最后, 它计算这两个向量的点积, 如果它是大于0, 它将变量 "above" 设置为 true, 如果它小于或等于 0, 它将其设置为 "false "。